<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/27
 * Time: 14:19
 */

namespace app\admin\controller;


use app\Model\AdminModel;
use app\Model\AdminRoleModel;
use think\Collection;
//管理员管理
class Admin extends Common
{
    /*
     * 管理员管理 页面
     */
    public function index()
    {
//        $this->_initialize();

        $getAdmin = AdminModel::alias('a')
        ->join('tp_admin_role a_r','a.role_id=a_r.role_id')//角色表
        ->paginate(null,false,['query'=>request()->param()]);
//        dump($getAdmin);die();
        $data['data'] = $getAdmin;
        $this->getConAndAct('access');
        return view('index',$data);
    }
    /*
     * 新增/编辑管理员页面
     */
    public function add()
    {
        //角色列表
        $getRole = AdminRoleModel::select();
        $data['data'] = $getRole;
        $getData = input('get.');
        if ($getData){
            $getAdmin = AdminModel::alias('a')
                ->join('tp_admin_role a_r','a.role_id=a_r.role_id')//角色表
                ->find($getData['admin_id']);//获取该管理员信息
            $data['admin']=$getAdmin;
            return view('edit',$data);//编辑页面

        }
        return view('add',$data);//新增页面
    }
    /*
     * 新增/编辑管理员
     */
    public function addData()
    {
        $postData = input('post.');
        $setData = [
            'user_name'=>$postData['user_name'],//管理员账号
            'email'=>$postData['email'],//邮箱
//            'password'=>encryption($postData['password']),//密码
            'role_id'=>$postData['role_id'],//角色id
        ];
        if (isset($postData['forbidden'])){//禁用、启用
            $setData['forbidden']=1;
        }else{
            $setData['forbidden']=0;
        }
        if (isset($postData['admin_id'])){
            sole('tp_admin',['user_name'=>$postData['user_name']],['admin_id',$postData['admin_id']]);//判断修改账号是否存在
            if ($postData['password']!='0000'){//编辑密码
                $setData['password']=encryption($postData['password']);//密码加密
            }
            $setAdmin = AdminModel::update($setData,['admin_id'=>$postData['admin_id']]);//编辑
        }else{

            sole('tp_admin',['user_name'=>$postData['user_name']]);//判断账号是否存在
            $setData['password']=encryption($postData['password']);//密码加密
            $setData['add_time']=time();//加入时间

            $setAdmin = AdminModel::insert($setData);//新增
        }
        return setDatabase($setAdmin);
    }
    /*
     * 删除管理员
     */
    public function del(){
        $postData = input('post.');


        $delRole = AdminModel::destroy($postData['id']);
        return setDatabase($delRole);
    }
//实验view str_repeat 无限分类
    public function cs()
    {
        $data['menu']=['a'=>'a1','b'=>'b1','c'=>'c1'];
        return view('cs',$data);
    }
}